package com.szewec.ps.ui.common;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.ListBaseAdapter;
import com.szewec.ps.base.SuperViewHolder;
import com.szewec.ps.databinding.ActivitySearchSurveyObjectBinding;
import com.szewec.ps.model.task.SurveyObjectModel;
import com.szewec.ps.util.LogUtil;
import com.szewec.ps.util.PreferincesContext;
import com.szewec.ps.util.SharedPreferencesUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.okhttp.OkHttp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchSurveyObjectActivity extends BaseActivity {
    private ActivitySearchSurveyObjectBinding binding;
    private String projectId;
    private List<SurveyObjectModel> surveyObjectModelList = new ArrayList<>();
    /**
     * recycleView相关
     */
    private DataAdapter dataAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search_survey_object);
        ButterKnife.bind(this);

        projectId = (String) SharedPreferencesUtil.getData(PreferincesContext.PROJECT_ID,"");

        initTextChangeListener();
        initView();
    }

    private void initTextChangeListener(){
        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String name = editable.toString();
                if (StringUtils.isEmpty(name)){
                    binding.clHistory.setVisibility(View.VISIBLE);
                    surveyObjectModelList.clear();
                    binding.clSearchResult.setVisibility(View.GONE);
                    return;
                }else {
                    binding.clHistory.setVisibility(View.GONE);
                    binding.clSearchResult.setVisibility(View.VISIBLE);
                }
                querySurveyObject(name);
            }
        });
    }

    private void initView() {
        dataAdapter = new DataAdapter(this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(dataAdapter);
        binding.lrvSurveyObject.setAdapter(lRecyclerViewAdapter);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        binding.lrvSurveyObject.setLayoutManager(linearLayoutManager1);
        binding.lrvSurveyObject.setLoadMoreEnabled(false);
        binding.lrvSurveyObject.setPullRefreshEnabled(false);
    }

    private void querySurveyObject(String name){
        HashMap<String,Object> param = new HashMap<>();
        param.put("projectId",projectId);
        param.put("nameLike",name);
        OkHttp.get("/tpms-plan-service/api/task/get-survey-object-by-params", param, null, new OkHttp.MyCallback() {
            @Override
            public void onSuccess(String result) {
                LogUtil.i("查询调查对象结果:",result);
                JSONArray data = JSONObject.parseObject(result).getJSONArray("data");
                if (data != null && data.size() > 0){
                    surveyObjectModelList = data.toJavaList(SurveyObjectModel.class);
                }else {
                    surveyObjectModelList.clear();
                }
                runOnUiThread(()->{
                    dataAdapter.setDataList(surveyObjectModelList);
                    dataAdapter.notifyDataSetChanged();
                });
            }
        });
    }

    private void doSelected(SurveyObjectModel surveyObjectModel){
        LogUtil.i("选择的是:",surveyObjectModel.getSurveyName());
        Intent intent = new Intent();
        intent.putExtra(Constants.EXTRA_KEY.SEARCH_SURVEY_OBJECT,surveyObjectModel);
        setResult(Constants.RESULT_CODE.SEARCH_SURVEY_OBJECT,intent);
        finish();
    }

    @OnClick({R.id.tv_cancel,R.id.iv_clear_history})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.iv_clear_history:
                break;
        }
    }
    private class DataAdapter extends ListBaseAdapter<SurveyObjectModel> {


        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_search_result;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView title = holder.getView(R.id.tv_name);

            SurveyObjectModel model = mDataList.get(position);
            title.setText(model.getSurveyName());
            //行点击监听事件
            holder.itemView.setOnClickListener(view -> {
                doSelected(model);
            });
        }
    }

}
